<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>确认充值</title>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/plugins/jQuery/themes/default/easyui.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/plugins/jQuery/themes/icon.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/plugins/jQuery/demo.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/admin/css/form.css"/>
	
	<script src="${pageContext.request.contextPath }/plugins/jQuery/jquery.min.js"></script>
	<script src="${pageContext.request.contextPath }/plugins/jQuery/jquery.easyui.min.js"></script>
	<script src="${pageContext.request.contextPath}/plugins/jQuery/easyui-lang-zh_CN.js"></script>
	
	<script type="text/javascript">
	
	$(function(){

			loadTable();
			$('#dlg').dialog('close');
	 });
		
		function loadTable(){
		   var acct_id = '${acct_id}';
			$('#dg').datagrid({
				rownumbers:true,
				singleSelect:true,
				pagination:true,
				pageSize:10,
				pageList:[10,20,30,50,100],
		 		url:'${pageContext.request.contextPath }/admin/acct/rechargelist?acct_id='+acct_id,
	    		columns:[[
	    		    {field:'create_time',title:'时间',align:'center',width:160},
					{field:'cust_name',title:'客户姓名',align:'center',width:80},
					{field:'cust_sex',title:'性别',align:'center',width:40,formatter: function (value, row, index) {
						var res="";
						if(value==0){
							res="男";
						}else if(value==1){
							res= "女";
						}
						return res;
					}},
					{field:'cust_identity',title:'身份证号',align:'center',width:130},
					{field:'mobile',title:'联系电话',align:'center',width:100},
					{field:'activity_name',title:'活动名称',align:'center',width:180},
					{field:'pre_amount',title:'预充金额',align:'center',width:100,formatter: function (value, row, index) {
						return "￥"+value;
					}},
					{field:'activity_amount',title:'活动余额',width:130,align:'center',formatter: function (value, row, index) {
						return "￥"+value;
					}},
					{field:'trade_status',title:'充值状态',width:80,align:'center',formatter: function (value, row, index) {
						var cstatus="";
						if(value=="02"){
							cstatus="<span style='color:green;'>充值成功</span>";
						}else if(value=="01"){
							cstatus="预充值";
						}else if(value=="03"){
							cstatus="<span style='color:red;'>关闭充值</span>"
						}
						return cstatus;
					}},
					{field:'operation',title:'操作选项',width:200,align:'left',formatter: function (value, row, index) {
						var str = '';
						
						if(row.trade_status=='00' || row.trade_status=='01'){
						   	str+=' <a href="javascript:open(\''+row.sn+'\',\''+row.activity_sn+'\',\''+row.cust_id+'\',\''+row.acct_id+'\',\''+row.pre_amount+'\',\''+row.activity_amount+'\')" name="recharge"  class="easyui-linkbutton" ></a>';
							str+=' <a href="javascript:reclose(\''+row.sn+'\',\''+row.activity_sn+'\')" name="reclose"  class="easyui-linkbutton" ></a>';
						}
						
						if(row.trade_status !='02'){
						// 	str+=' <a href="javascript:reclose(\''+row.sn+'\',\''+row.activity_sn+'\')" name="reclose"  class="easyui-linkbutton" ></a>';
						}
						return str;
					}}
	 			]],
				onLoadSuccess:function(data){  
			        $("a[name='recharge']").linkbutton({text:'<span style="color:red;">确认充值</span>',plain:true,iconCls:'icon-ok'});
			        $("a[name='reclose']").linkbutton({text:'关闭充值',plain:true,iconCls:'icon-cancel'});
			        
			        //添加“合计”列
		            $('#dg').datagrid('appendRow', {
				                  activity_name: '<span class="subtotal" style="font-weight:bold;">汇总统计</span>',
					              pre_amount: '<span class="subtotal" style="font-weight:bold;">' + compute("pre_amount") + '</span>',
					              activity_amount: '<span class="subtotal" style="font-weight:bold;">' + compute("activity_amount") + '</span>'
				   });
				}
			});
		}
		
		 //指定列求和
        function compute(colName) {
            var rows = $('#dg').datagrid('getRows');
            var total = 0;
            for (var i = 0; i < rows.length; i++) {
                var val = rows[i][colName];
                //var str = val.replace(/,/, "");
                total += parseFloat(val);
            }
            return total;
        }
		
		//预充值
		function open(sn,activity_sn,cust_id,acct_id,pre_amount,activity_amount){
		    $("#sn").val(sn);
		    $("#activity_sn").val(activity_sn);
		    $("#cust_id").val(cust_id);
		    $("#acct_id").val(acct_id);
		    $("#pre_amount").val(pre_amount);
		    $("#activity_amount").val(activity_amount);
		    $('#dlg').dialog('open').dialog('setTitle','系统消息');
		    
		}
		
		function recharge(){
		  var result=false;
		  var pwd = $("#pwd").textbox('getValue');
		  if(pwd == null || pwd == '')
		  {
		     $.messager.alert('系统消息','<span style=\"color:red;\">验证密码，不能为空！</span>');	 
		     return ;
		  }
		  var sn = $("#sn").val();
		  var activity_sn = $("#activity_sn").val();
		  var cust_id = $("#cust_id").val();
		  var acct_id = $("#acct_id").val();
		  var pre_amount = $("#pre_amount").val();
		  var activity_amount = $("#activity_amount").val();
		  /*
		  $.post("${pageContext.request.contextPath}/admin/acct/recharge",{sn:sn,pwd:pwd,activity_sn:activity_sn,cust_id:cust_id,acct_id:acct_id,pre_amount:pre_amount,activity_amount:activity_amount},
				function(data){
					if(data=="0"){
						$.messager.alert('系统消息','确认充值成功！<br/>');
				//		 parent.window.findData();
						 $('#dlg').dialog('close');
						 $('#dg').datagrid('reload');
				//		loation.href="${pageContext.request.contextPath}/admin/acct";
						result=true;
						alert(result);
					}else if(data=="2"){
					    $.messager.alert('系统消息','<span style=\"color:red;\">验证密码错误，请稍后再试！</span>');	 
					}else{
						$.messager.alert('系统消息','<span style=\"color:red;\">确认充值失败，请稍后再试！</span>');	 
					}
				},"text");
			*/
			$.ajax({
				url : "${pageContext.request.contextPath}/admin/acct/recharge",
				async : false, // 注意此处需要同步，因为返回完数据后，下面才能让结果的第一条selected
				type : "POST",
				data : {sn:sn,pwd:pwd,activity_sn:activity_sn,cust_id:cust_id,acct_id:acct_id,pre_amount:pre_amount,activity_amount:activity_amount},
				success : function(data) {
					if(data=="0"){
						$.messager.alert('系统消息','确认充值成功！<br/>');
				//		 parent.window.findData();
						 $('#dlg').dialog('close');
						 $('#dg').datagrid('reload');
				//		loation.href="${pageContext.request.contextPath}/admin/acct";
						result=true;
					}else if(data=="2"){
					    $.messager.alert('系统消息','<span style=\"color:red;\">验证密码错误，请稍后再试！</span>');	 
					}else{
						$.messager.alert('系统消息','<span style=\"color:red;\">确认充值失败，请稍后再试！</span>');	 
					}
				}
			});
		  if(result){
		  	window.setTimeout(goback,2500);	  // 延时3秒钟自动跳转回到客户充值页面	
		  }
		}
		
		// 跳转回到客户充值列表
		function goback(){
			location.href="${pageContext.request.contextPath}/admin/acct";
		}
		
		// 关闭客户充值
		function reclose(sn,activity_sn){
		   	$.messager.confirm('系统消息',"您确定要关闭充值吗？", function(r){
	   			if(r){
	   				$.post("${pageContext.request.contextPath}/admin/acct/reclose",{sn:sn,activity_sn:activity_sn},
					function(data){
						if(data=="1"){
							$.messager.alert('系统消息','关闭充值成功！<br/>');
							$('#dg').datagrid('reload');
						}else{
							$.messager.alert('系统消息','<span style=\"color:red;\">关闭充值失败，请稍后再试！</span>');	 
						}
					},"text");
	   			}else{
	   				$.messager.alert('系统消息','<span style=\"color:green;\">您取消了关闭充值操作！</span>');
	   			}
	   		});  
		}
		
		function showImg(imgUrl){
			$('#dlg').dialog('open');
			var image=document.getElementById("stationImg");
			var src="${pageContext.request.contextPath }"+imgUrl;
			image.setAttribute("src",src );
		}

		// 打开一个新的选项卡，并制定选项卡的名称
		function show(url,name){
			parent.window.addTab(name,url);
		}
		
		function findData(){
			loadTable();
		}

	</script>
	<style type="text/css">
		body{
		font-family:'微软雅黑';}
	</style>
  </head>
  
  <body>
	<table id="dg" toolbar="#searchtool" style="width:1250px;height:400px;"></table>
	<div id="searchtool" style="padding:5px;height:auto;text-align:right;">
	     <a href="javascript:location.href='${pageContext.request.contextPath}/admin/acct'" class="easyui-linkbutton" data-options="iconCls:'icon-back'" style="width:120px;margin:0px 3px;">返回客户列表</a>
    </div>
	
    <div id="dlg" class="easyui-dialog" style="width:400px;height:150px;padding:10px 20px" closed="true" buttons="#dlg-buttons">
	  <form id="fm" method="post">
			<div class="fitem">
				
				<input id="pwd" name="pwd" type="password" class="easyui-textbox" style="width:180px;height:26px" placeholder="请输入充值密码"/>
				<input id="sn" name="sn" type="hidden" />
				<input id="activity_sn" name="activity_sn" type="hidden" />
				<input id="cust_id" name="cust_id" type="hidden" />
				<input id="acct_id" name="acct_id" type="hidden" />
				<input id="pre_amount" name="pre_amount" type="hidden" />
				<input id="activity_amount" name="activity_amount" type="hidden" />
			</div>
			<div id="dlg-buttons">
				<a href="#" class="easyui-linkbutton" id="add" iconCls="icon-ok" onclick="recharge()">验证</a>
			</div>
		</form>
   </div>
  
	
  </body>
</html>
